########## todo: display stats
import player
import hashlib
playerc = player.player()
##################################
#commands as functions go here####
##################################
####### future: update all functions to return two things
##### output, changes
####### for now, plan is to implement like gb pokemon
### assign effects and changes a value, this will be int for now
#### add a deque in player dict to act as a queue for changes and effects
def help():
    print("helper")
    return "helpstuff", []
def farts():
    print("fart test")
    
    ### TODO: return an actual value print(self.fartsval)
    return "fartsval: "  +"""
You take a deep breath
and fart.

""", "farted"
#def debug_hpup():
#will statchange up and display a message
#class cprocessor():
    #def huh(self):
        #pass
class commands():

    
    loginplaceholder = """
     @@@@@    Welcome to
   @@@@@@@@0       __  __         _    _        __  __               
  @@@@@@@@@00     |  \/  |_  _ __| |__| |_  _  |  \/  |___  ___ _ _  
 @@@@@@@@@000*    | |\/| | || / _` / _` | || | | |\/| / _ \/ _ \ ' \ 
  @@@@@@0000*     |_|  |_|\_,_\__,_\__,_|\_, | |_|  |_\___/\___/_||_|
   @0000000*                             |__/                        
     0000*            Tentatively, anyway

___Login?(login)        ____ Create a new character?(new) ___Guest?(guest)


"""
    #commands dict
	#TODO: eventually tie things together so that commands can do actual actions
    ##############################################################################################3
    #See dostuff below? ALL COMMANDS HAVE TO GO THERE
    #moredoc later
    #
    #
    #
    #
    #
    #
    #
    #########################################################################

    dostuff = {'help': help,'fart':farts,'buy':'stuff'}
    
    ##########################################################################################
    guestmessage = "Welcome, guest? read some eula or some stuff somewhere. don't be a jerk"
    guestnum = 0
    tryagainmsg = "Try again, stuff is case sensitive"
    def __init__(self):
        pass
    def read_input(self,dict):
        #takes input and reads the dict?
        #data
        if dict['connection_mode'] == 0:
            #banner to login mode
            dict['out'].append(self.loginplaceholder)
            dict['connection_mode'] = 1
        if dict['connection_mode'] == 1:
            #Login mode
            if dict['data']:
                holding = dict['data'].popleft().decode('utf-8')
                inp = str(holding.strip('\n'))
                if inp == 'login':
                  
                    dict['out'].append("please type your username and password as \"username password\"" + "\n")
                    dict['connection_mode'] = 9
                if inp == 'new':
                    
                    dict['newuserflag'] = 0
                    dict['connection_mode'] = 8
                if inp == 'guest':
                    dict['out'].append(self.guestmessage + "\n")
                    
                    guestname = "Guest"+ str(self.guestnum)
                    self.guestnum += 1
                    dict['pdata']['character_a']= guestname
                    dict['pdata']['user_name']= guestname
                    dict['pdata']['p_power'] = 1
                    dict['pdata']['p_health']  =1
                    dict['pdata']['p_base_heatlth'] = 1
                    dict['pdata']['p_modifier'] = 1
                    dict['out'].append("You are "+ guestname+"\n")
                    dict['connection_mode'] = 2
                else:
                    if dict['connection_mode'] == 1:
                        dict['out'].append(self.tryagainmsg + "\n")
            else:
                pass
        if dict ['connection_mode'] == 8:
############################################################            ####### new characterstuff
            if dict['newuserflag'] == 0:
                dict['out'].append('What is your user name? (not your charcter\'s!)' + "\n")
                dict['newuserflag'] = 1
                
            if dict['newuserflag'] == 1:
                if dict['data']:
                    ### TODO: filter more, require 1 word no spaces?
                    holdingnewu = dict['data'].popleft().decode('utf-8')
                    inpnewu = str(holdingnewu.strip('\n'))
                    if ' ' in inpnewu == True:
                        dict['out'].append('no spaces'+"\n")
                    elif len(inpnewu) > 20:
                        dict['out'].append('too long?'+"\n")
                    else:
                        if playerc.user_exists(inpnewu):
                            dict['out'].append('Username is taken'+"\n")
                            
                        else:
                            dict['pdata_temp']['username'] = inpnewu
                            dict['out'].append('Is "'+inpnewu+'" correct?'+"\n")
                            dict['newuserflag'] = 2
            if dict['newuserflag'] ==2:
                
                if dict['data']:
                    holdingconf = dict['data'].popleft().decode('utf-8')
                    inpnewconf = str(holdingconf.strip('\n'))
                    if inpnewconf == 'yes':
                        dict['out'].append('What is your password?' + "\n")
                        dict['newuserflag'] = 3
                        
                    elif inpnewconf == 'no':
                        dict['newuserflag'] = 0
                    else:
                        dict['out'].append(self.tryagainmsg + "\n")
            if dict['newuserflag'] == 3:
                if dict['data']:
                    holdingpass = dict['data'].popleft().decode('utf-8')
                    inpnewpass = str(holdingpass.strip('\n'))
                    if ' ' in inpnewpass == True:
                        dict['out'].append('no spaces, try again' + "\n")
                    else:
                        #hash input, pass to maria db.
                        #AFTER THIS WRITE, login theoretically can work, but is missing character name?? maybe make way to change/add charcter name later in case of error
                        
                            #newpass = hashlib.sha3_256()
                        #### SHA3 only in python 3.6+, so maybe switch later???
                        
                        newpass = hashlib.sha256(inpnewpass.encode("utf-8")).hexdigest()
                        #encode inpnewpass into bytes???

                        dict['pdata_temp']['passholder'] = str(newpass)
                        dict['newuserflag'] = 4

            if dict['newuserflag'] == 4:
                dict['out'].append('What is your charcter\'s name?' + "\n")
                dict['newuserflag'] = 5
                
            if dict['newuserflag'] == 5:
                if dict['data']:
                    holdingnewch = dict['data'].popleft().decode('utf-8')
                    inpnewch = str(holdingnewch.strip('\n'))
                    
                  
                    dict['out'].append('Is "'+inpnewch+'" correct?' + "\n")
                    dict['pdata_temp']['char'] = inpnewch                                         
                    dict['newuserflag'] = 6
            if dict['newuserflag'] ==6:
                
                if dict['data']:
                    holdingconf = dict['data'].popleft().decode('utf-8')
                    inpnewconf = str(holdingconf.strip('\n'))
                    
                    if inpnewconf == 'yes':
                        dict['newuserflag'] = 9
                        playerc.adduser(dict['pdata_temp']['username'],dict['pdata_temp']['passholder'],dict['pdata_temp']['char'])
                    if inpnewconf == 'no':
                        dict['newuserflag'] = 4
            if dict['newuserflag'] == 9:
                dict['pdata']=playerc.getdatadict(dict['pdata_temp']['username'])
                #generate some other data
                #display a message?
                #remove the new user flag? or could be useful later?
                
                dict['connection_mode'] = 2
                
        if dict ['connection_mode'] == 9:
            ###################################################### login stuff
             if dict['data']:
                    holdinglogn = dict['data'].popleft().decode('utf-8')
                    inplogon = str(holdinglogn.strip('\n'))
                    try:
                        inpuname, inppass = inplogon.split()
                    except ValueError:
                        dict['out'].append('try again, it\'s username password')
                    holdpass = hashlib.sha256(inppass.encode("utf-8")).hexdigest()

                    #seperate data uname pass pair and check
                    
                    if playerc.loginproc(inpuname,str(holdpass)):
                        dict['pdata']= playerc.getdatadict(inpuname)
                        dict['out'].append( str(dict['pdata']['p_health'])+'/'+str(dict['pdata']['p_base_health']) +"HP"+"\n")
                        dict['connection_mode'] = 2

                    else:
                        dict['out'].append('try again' +"\n")

        if dict ['connection_mode'] == 2:
            #play mode 1 stuff
            if dict['data']:
                holding = dict['data'].popleft().decode('utf-8')
                inp = str(holding.strip('\n'))
                if inp not in self.dostuff:
                    dict['out'].append( 'boop: try again' + "\n")
                else:
                    #dict['out'].append(self.dostuff[inp]() +"\n")
                    #gets and output and changes from dostuff
                    cmdout, changes = self.dostuff[inp]()
                    dict['out'].append(cmdout)
                    dict['events'].append(changes)
        else:
            pass
                                       
       


	#hmmm how to handle actual actions.
	# stuff like help can return strings, but other things need to have actions 
	# or be strings that need to look a certain way. "player farts" or "player does x to other_player"
	# before I had a dict that changed a state, I guess I could do that
	# dict with a state number
	# state number tells what to do in another function
	# then that function makes stuff to return.
###########################################################################
#######################################################################


